package com.magicborrow.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.magicborrow.Constants;
import com.magicborrow.R;
import com.magicborrow.beans.BaseEntity;
import com.magicborrow.beans.UserBean;
import com.magicborrow.utils.MD5;
import com.magicborrow.utils.Md5Util;
import com.magicborrow.utils.UserTools;
import com.magicborrow.utils.VolleyTool;
import com.magicborrow.views.LoadingDialog;

import java.util.HashMap;

/**
 * Created by Administrator on 2016/5/15.
 */
public class VerifyPasswordActivity extends BaseActivity implements VolleyTool.HTTPListener{

    //修改密码
    private TextView tvCurrent;
    private EditText etCurrentPwd;
    private EditText etNewPwd;
    private EditText etConfirmPwd;
    private TextView tvConfirm;
    private LoadingDialog dialog;
    private UserBean.User user;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_verify_password);
        initView();
        initData();
        initListener();
    }

    private void initView(){
        tvCurrent = (TextView)findViewById(R.id.tv_currentAccount);
        etCurrentPwd = (EditText) findViewById(R.id.et_currentpwd);
        etNewPwd = (EditText) findViewById(R.id.et_newpwd);
        etConfirmPwd = (EditText) findViewById(R.id.et_confirmpwd);
        tvConfirm = (TextView)findViewById(R.id.confirm);
    }

    private void initData() {
        user = UserTools.getUser(this);
        if (null!= user){
            String mobile = user.getMobile();
            tvCurrent.setText(mobile);
        }
    }


    private void initListener(){
        tvConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                save();
            }
        });
        findViewById(R.id.iv_back).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

    }

    //保存配置
    private void save() {
        String currentPwd=etCurrentPwd.getText().toString().trim();
        String newPwd=etNewPwd.getText().toString().trim();
        String confirmPwd=etConfirmPwd.getText().toString().trim();
        if (TextUtils.isEmpty(currentPwd)){
            showShortMsg("当前密码不可以为空");
            return;
        }

        if (TextUtils.isEmpty(newPwd)){
            showShortMsg("新的密码不可以为空");
            return;
        }

        if (TextUtils.isEmpty(confirmPwd)){
            showShortMsg("确认的密码不可以为空");
            return;
        }
        if (!confirmPwd.equals(newPwd)){
            showShortMsg("两次密码不一致");
            return;
        }
        HashMap<String, String> params = new HashMap<>();
        params.put("acc_token", user.getAcc_token());
        params.put("oldPasswd", MD5.getMD5(MD5.getMD5(currentPwd)));
        params.put("newPasswd", MD5.getMD5(MD5.getMD5(newPwd)));
        dialog = new LoadingDialog(this);
        dialog.show();
        VolleyTool.get(Constants.URL_GET_MODIFY_PWD, params, this, Constants.REQUEST_ONE, BaseEntity.class);
    }

    @Override
    public <T> void onResponse(T t, int requestCode) {
        if (requestCode==Constants.REQUEST_ONE){
            BaseEntity entity= (BaseEntity) t;
            showShortMsg(entity.getMessage());
            if (entity.getCode()==0){
                finish();
            }
        }
        dialog.dismiss();
    }

    @Override
    public void onErrorResponse(VolleyError error, int requestCode) {
        dialog.dismiss();
    }
}